package com.maple.exclusivesport;

import android.app.Activity;
import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.view.SurfaceView;
import android.view.View;

import java.io.File;
import java.util.UUID;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class JournalActivity extends Activity {

    @Bind(R.id.surfaceViewColour)
    SurfaceView surfaceViewColour;
    CameraColour cameraColour;
    AudioManager meng;
    MediaPlayer mShootSound;
    PersonInfoDialog personInfoDialog;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_journal);
        ButterKnife.bind(this);
        context = this;
        //创建文件夹
        File dir = new File(Constants.CACTH_PHOTO_PATH);
        if (!dir.exists())
            dir.mkdirs();

        meng = (AudioManager) this.getSystemService(Context.AUDIO_SERVICE);
        int volume = meng.getStreamVolume(AudioManager.STREAM_NOTIFICATION);
        if (volume != 0) {
            if (mShootSound == null) {
                mShootSound = MediaPlayer.create(this, Uri.parse("file:///system/media/audio/ui/camera_click.ogg"));
            }

        }
        cameraColour = new CameraColour(surfaceViewColour.getHolder());
        personInfoDialog = new PersonInfoDialog(context);
    }

    @OnClick({R.id.surfaceViewColour, R.id.reset_picture, R.id.confirm_picture, R.id.switch_camera})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.confirm_picture:
                deletePicture();
                finish();
                break;
            case R.id.surfaceViewColour:
                if (mShootSound != null) {
                    mShootSound.start();
                }
                cameraColour.takePicture();
                personInfoDialog.show();
                break;
            case R.id.switch_camera:
                deletePicture();
                Constants.cameraIndex = Constants.cameraIndex == 1 ? 0 : 1;
                cameraColour.stopPreview();
                cameraColour.startPreview();
                break;
            case R.id.reset_picture:
                deletePicture();
                cameraColour.stopPreview();
                cameraColour.startPreview();
                break;
        }
    }

    public void deletePicture()
    {
        /**删除刚刚保存的照片**/
        File file = new File(Constants.filePath);
        if (file.exists()) {
            file.delete();
        }
    }

}
